package com.xuecheng.content.controller;

import com.xuecheng.api.content.CourseBaseApi;
import com.xuecheng.api.content.model.dto.CourseBaseDTO;
import com.xuecheng.api.content.model.qo.QueryCourseModel;
import com.xuecheng.api.content.model.vo.CourseBaseVO;
import com.xuecheng.common.domain.code.CommonErrorCode;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.exception.ExceptionCast;
import com.xuecheng.content.common.util.UAASecurityUtil;
import com.xuecheng.content.convert.CourseBaseConvert;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.content.service.CourseBaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;


/**
 * <p>
 * 课程基本信息 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
public class CourseBaseController implements CourseBaseApi {

    @Autowired
    private CourseBaseService courseBaseService;

    /*
     * SpringMVC 他的默认传参方式为：queryString
     *   如果是其他的传入参数形式必须要加上注解
     *       json格式请求体：@RequestBody
     *       Restful：@PathVariable
     * */
    @PostMapping("course/list")
    public PageVO queryCourseList(PageRequestParams params,
                                  @RequestBody QueryCourseModel model) {
        // 1.获得请求头中的令牌并解析拿到companyId值
        Long companyId = UAASecurityUtil.getCompanyId();
        if (ObjectUtils.isEmpty(companyId)) {
            ExceptionCast.cast(CommonErrorCode.E_100108);
        }
        // 2.调用service层获得结果数据
        PageVO pageVO = courseBaseService.queryCourseList(params, model, companyId);
        return pageVO;
    }

    @PostMapping("course")
    public CourseBaseDTO createCourseBase(@RequestBody CourseBaseVO courseBaseVO) {
        //vo-->dto
        CourseBaseDTO courseBaseDTO = CourseBaseConvert.INSTANCE.vo2dto(courseBaseVO);

        //获得登陆后的公司id值
        Long companyId = UAASecurityUtil.getCompanyId();
        courseBaseDTO.setCompanyId(companyId);

        return courseBaseService.createCourseBase(courseBaseDTO);
    }

    @GetMapping("course/{courseBaseId}")
    public CourseBaseDTO getCourseBaseById(@PathVariable Long courseBaseId) {
        //获得公司的id值
        Long companyId = UAASecurityUtil.getCompanyId();
        return courseBaseService.getCourseBaseById(courseBaseId, companyId);
    }

    @PutMapping("course")
    public CourseBaseDTO modifyCourseBaseById(@RequestBody CourseBaseVO courseBaseVO) {

        // 1.将vo转为dto数据
        CourseBaseDTO courseBaseDTO = CourseBaseConvert.INSTANCE.vo2dto(courseBaseVO);
        // 2.获得公司的id值
        Long companyId = UAASecurityUtil.getCompanyId();
        courseBaseDTO.setCompanyId(companyId);
        // 3.调用service获得dto结果
        return courseBaseService.modifyCourseBaseById(courseBaseDTO);
    }

    @DeleteMapping("course/{courseBaseId}")
    public void removeCourseBase(@PathVariable Long courseBaseId) {

        //获得公司id
        Long companyId = UAASecurityUtil.getCompanyId();

        courseBaseService.removeCourseBase(courseBaseId, companyId);
    }

    @GetMapping("course/commit/{courseBaseId}")
    public void submitCourseBase(@PathVariable Long courseBaseId) {

        //获得公司id
        Long companyId = UAASecurityUtil.getCompanyId();
        courseBaseService.submitCourseBase(courseBaseId, companyId);
    }

    /*
     * 课程预览：
     *   要使用SpringMVC+Freeamarker作为结果视图器
     *       1.数据模型
     *       2.结果视图的名称
     *       可以使用SpringMVC提供的ModelAndView
     * SpringMVC多结果视图器的并存
     *   1.Json的结果视图器（jackson）
     *       使用@RestController
     *       使用@Responsebody
     *   2.SpringMVC+Freeamarker作为结果视图器
     *       1.导入Freemarker的依赖
     *       2.配置Freemarker（Apollo）
     *       3.在Controller中使用ModelAndView
     * */
    @GetMapping("course/preview/{courseBaseId}/{companyId}")
    public Object preview(@PathVariable Long courseBaseId, @PathVariable Long companyId) {

        //1.从业务层获得数据模型
        Map<String, Object> dataMap = courseBaseService.preview(courseBaseId, companyId);

        //2.结果视图器
        ModelAndView modelAndView = new ModelAndView("learing_article", dataMap);
        return modelAndView;
    }

    @PostMapping("course_pub/publish/{courseBaseId}")
    public void publish(@PathVariable Long courseBaseId) {

        //获得公司id
        Long companyId = UAASecurityUtil.getCompanyId();

        courseBaseService.publish(courseBaseId, companyId);
    }

}
